<?php
namespace backend\models\activeRecords;

use Yii;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "{{%t_pay}}".
 *
 * @property int $pay_id
 * @property string $pay_order_no
 * @property string $pay_order_no_cooperate 合作商户订单号
 * @property string $pay_type
 * @property string $pay_mode_id
 * @property int $pay_state
 * @property string $pay_user
 * @property string $pay_tel
 * @property int $pay_game_id
 * @property int $pay_server_id
 * @property string $pay_game_user
 * @property string $pay_game_role
 * @property double $pay_money
 * @property string $pay_money_with_rate 去掉费率后的金额
 * @property int $pay_time
 * @property string $pay_ip
 * @property string $card_id 充值卡名
 * @property int $coupon_id 返利券id
 * @property string $pay_game_name 游戏名
 * @property string $pay_server_name 游戏区服名
 * @property string $remark 充值备注
 */
class TPay extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%t_pay}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['pay_game_id', 'pay_server_id','pay_money','pay_game_user'], 'required'],
            [['pay_state', 'pay_game_id', 'pay_server_id', 'pay_time', 'coupon_id'], 'integer'],
            [['pay_money', 'pay_money_with_rate'], 'number'],
            [['pay_order_no', 'remark'], 'string', 'max' => 255],
            [['pay_order_no_cooperate', 'pay_type', 'pay_mode_id', 'pay_user', 'pay_tel', 'pay_game_user', 'pay_game_role', 'pay_ip', 'pay_game_name', 'pay_server_name'], 'string', 'max' => 50],
            [['card_id'], 'string', 'max' => 10],
            [['pay_order_no'], 'unique'],
        ];
    }

    public function getUser()
    {
        return $this->hasOne(TUser::className(),['username'=>'pay_game_user']);
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'pay_id' => Yii::t('app', 'Pay ID'),
            'pay_order_no' => Yii::t('app', 'Pay Order No'),
            'pay_order_no_cooperate' => Yii::t('app', '合作商户订单号'),
            'pay_type' => Yii::t('app', 'Pay Type'),
            'pay_mode_id' => Yii::t('app', 'Pay Mode ID'),
            'pay_state' => Yii::t('app', 'Pay State'),
            'pay_user' => Yii::t('app', 'Pay User'),
            'pay_tel' => Yii::t('app', 'Pay Tel'),
            'pay_game_id' => Yii::t('app', '充值游戏'),
            'pay_server_id' => Yii::t('app', '充值区服'),
            'pay_game_user' => Yii::t('app', '充值账号'),
            'pay_game_role' => Yii::t('app', '游戏角色'),
            'pay_money' => Yii::t('app', '充值金额'),
            'pay_money_with_rate' => Yii::t('app', '去掉费率后的金额'),
            'pay_time' => Yii::t('app', '支付时间'),
            'pay_ip' => Yii::t('app', 'Pay Ip'),
            'card_id' => Yii::t('app', '充值卡名'),
            'coupon_id' => Yii::t('app', '返利券id'),
            'pay_game_name' => Yii::t('app', '游戏名'),
            'pay_server_name' => Yii::t('app', '游戏区服名'),
            'remark' => Yii::t('app', '充值备注'),
        ];
    }

    public function createOrderNo()
    {
        return str_replace("-", "", date("Y-m-dH-i-s")) . rand(1000, 2000);
    }

    public function payToGame($model){
        $game = TGame::findOne($model->pay_game_id);
        $key = 'd1e6a13079d30b65a99b5d9a67d047dc';
        $data['username'] = $model->pay_game_user;
        $data['game_id'] = $model->pay_game_id;
        $data['server_id'] = $model->pay_server_id;
        $data['pid'] = 'guild';
        $data['money'] = $model->pay_money;
        $data['coin'] = $game->game_money_per*$model->pay_money;
        $data['order'] = $this->createOrderNo();
        ksort($data);
        $data['sign'] = md5(http_build_query($data).$key);
        $data['time'] = time();
        $url = 'http://www.uquq.com/game/game-open-api/recharge-guild';

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }


}
